
/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
#include <pthread.h>
class Solution {
public:
    ListNode* EntryNodeOfLoop(ListNode* pHead) {
        if (!pHead || !pHead->next)
            return nullptr;
        ListNode* slow = pHead;
        ListNode* fast = pHead;

        while (fast && fast->next)
        {
            slow = slow->next;
            fast = fast->next->next;
            if (slow == fast)
                break;
        }
        if (slow != fast)
            return nullptr;
        while (pHead != slow)
        {
            pHead = pHead->next;
            slow = slow->next;
        }
        return slow;
    }
};